<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Location extends Frontend_Controller
{

	public function __construct()
	{
		parent::__construct();
		$this->template->set_layout('default');
		$this->load->model(array('browse_model'));

		//--PARTIALS--//
		$this->template->set_partial('header', 'header/headerTop');
        $this->template->set_partial('footer', 'footer/footer');

        $this->load->model('gallery_model');
	}

	
	
	
	function search($data,$offset=0,$limit=1){

		$locID = $data["tmpLOC"];
		
		if(isset($data['tmpCAT'])){
			$catID = $data['tmpCAT'] ;
		}else{
			$catID = 0 ;
		}


		//--LOCATION ---//
		if( isset($locID) && $locID !=0 ){
			$data['locData'] = $this->location_model->getPK($locID);
			$data['location'] = $data['locData']['name'];
		}else{
			$data['locData'] = array('name' => config_item('country'));
			$data['location'] =  config_item('country');
			
			
		}

		//--CATEGORY --//
		if( isset($catID) && $catID !=0 ){
			$data['catData']['info'] = $this->category_model->getPK($catID);
		}else{
			$dd = array('name' => 'Business');
			$data['catData'] = $dd;
		}


		$data['category'] = $this->category_model->getCetegory(0,12,TRUE);
		$data['state'] =  $this->location_model->getLocation(0,99);
		$data['city'] =  $this->location_model->find($deep = 1,$limit = count($data['state']), TRUE);

		/*-- 1 = Banner shows in INDEX page and 5 = Banner Shows Limit 5 */
		$data['banner'] = $this->gallery_model->getBanner(1,5);
		
		$data['post'] = $this->browse_model->getPost($locID,$catID,0,10);
		$this->template->build('welcome/home',$data);

	}
	
	
	//--Get City --rahul--//
	function getcity($stateID){
		header('Content-Type: application/x-json; charset=utf-8');
		echo(json_encode($this->location_model->getCitybystate($stateID)));
		die();
	}

	function set_location()
	{
		//--STORE COOKIE --///

		$url = $this->input->post('modal_curl');
		$cityID = $this->input->post('modal_city');
		
		//print $url; die();
		
		$cityData = $this->location_model->getPK($cityID);
		$cityName = $cityData['name'];
		
		
		$this->input->set_cookie("cook_city", $cityID, time()+3600*30);
		$this->input->set_cookie("cook_cityname", $cityName, time()+3600*30);
			
		//-- SET TEMP LOCATION --//
		write_session('tmpLOC',$cityData['id']);

		
		$eurl = explode('/', $url);
		$ctr = count($eurl);

		$vcity = seoUrl(($cityName));

		$resturl = $eurl[1];

		$finalurl = base_url().$vcity."/".$resturl;

		$message = $finalurl;
		$output = '{ "message": "'.$message.'"}';
		echo $output;
		
	}
	
	function set()
	{
		//--STORE COOKIE --///
	
		$url = $this->input->post('modal_curl');
		$cityID = $this->input->post('modal_city');
	
		//print $url; die();
	
		$cityData = $this->location_model->getPK($cityID);
		$cityName = $cityData['name'];
	
	
		$this->input->set_cookie("cook_city", $cityID, time()+3600*30);
		$this->input->set_cookie("cook_cityname", $cityName, time()+3600*30);
			
		//-- SET TEMP LOCATION --//
		
	
		$vcity = seoUrl(($cityName));
	
		
		$finalurl = base_url().$vcity;
	
		$message = $finalurl;
		$output = '{ "message": "'.$message.'"}';
		echo $output;
	
	}
	

	function clear_location(){


		redirect('browse/location/clear_city');
	}
	function clear_city(){


		if (isset($_COOKIE["cook_cityname"])){
			$this->input->set_cookie("cook_city", 0, 0);
			$this->input->set_cookie("cook_cityname", "", 0);

		}
		delete_cookie('cook_cityname');
	}
	
	
	function city_search(){
			
		$search = $_GET['term'];
	
		$query = $this->db->query("SELECT id,name,prefix FROM `location` WHERE `name` LIKE '%$search%' and id!=0 ORDER BY name ASC limit 15") or die('Something went wrong');
		$result = $query->result();
	
	
		$row_set = array();
	
		foreach ($result as $row1){
			$row = array(
					'label'	=> stripslashes($row1->name).', '.($row1->prefix),
					'value'	=> 	(int)$row1->id
			);
			$row_set[] = $row;//build an array
		}
	
		echo json_encode($row_set);//format the array into json data
	
	
	
	}
	
	function all()
	{
		//--WIDGET--//
		$data['mode'] = "LOCATION";
		$data['state'] =  $this->location_model->getLocation(0,99999);
		$data['location'] =  config_item('country');
	
		//BREADCRUMB//
		$this->template->set_breadcrumb('Home', '/');
		$this->template->set_breadcrumb('Location', 'location/all');
		$this->template->set_breadcrumb('All');
		
		$this->template->set_partial('header','header/headerTop');
		$this->template->set_layout('full_layout');
		$this->template->build('all', $data);
	}
	
	function city(){
		
		//--WIDGET--//
		$data['mode'] = "CITY";
		$data['adsense'] = $this->setting('adsense_pub_id');
		$data['widget_recent_listing'] = TRUE;
		$data['widget_popular_listing'] = TRUE;
		
		write_session('tmpLOC',null);
		write_session('tmpCAT',null);
		
		$data['state'] =  $this->location_model->getLocation(0,99999);
		
		$data['location'] =  config_item('country');
		
		
		$data['banner'] = $this->gallery_model->getBanner(1,5);
		
		//var_dump($data['banner']);die();
		//print_r($this->template);die();
		
		
		//print_r($data['category']);die();
		$data['city'] =  $this->location_model->find($deep = 1,$limit = count($data['state']), FALSE);
		$this->template->set_partial('header','header/headerTop');
		$this->template->set_layout('listing');
		$this->template->build('all', $data);
		
	}
	
}